package com.apache.log.es.config;

import com.apache.log.common.PropertiesConfig;
import com.apache.tools.StrUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 索引配置类
 * @author: Hou Dayu
 * @date: Created in 2020/11/18
 */
public class IndexConfig {

    public Map<String, String> getSettings(String aliases){
        Map<String, String> data = new HashMap();
        data.put("numberOfReplicas", PropertiesConfig.getApplicatinConfig("es.numberOfReplicas"));
        data.put("numbershards","5");
        if(StrUtil.isNotNull(aliases)) {
            data.put("aliases", aliases);
        }
        return data;
    }

    public String getMappings(String indexName) {
        String mappingStr = null;
        switch (indexName){
            case "logmodel":
                mappingStr = getLogmodelMapping();
                break;
            case "logmodeldetail":
                mappingStr = getLogmodeldetailMapping();
                break;
            case "logoperation":
                mappingStr = getLogoperationMapping();
                break;
        }
       return mappingStr;
    }

    private String getLogmodelMapping(){
        StringBuilder sb = new StringBuilder();
        sb.append("{\"doc\":{\"properties\":{");
        sb.append("\"modelId\":{\"type\":\"keyword\"}");
        sb.append(",\"modelEname\":{\"type\":\"keyword\"}");
        sb.append(",\"modelCname\":{\"type\":\"keyword\"}");
        sb.append(",\"modelBussiness\":{\"type\":\"keyword\"}");
        sb.append(",\"modelSysname\":{\"type\":\"keyword\"}");
        sb.append(",\"modelRemark\":{\"type\":\"keyword\"}");
        sb.append("}}}");
        return sb.toString();
    }

    private String getLogmodeldetailMapping(){
        StringBuilder sb = new StringBuilder();
        sb.append("{\"doc\":{\"properties\":{");
        sb.append("\"detailId\":{\"type\":\"keyword\"}");
        sb.append(",\"modelId\":{\"type\":\"keyword\"}");
        sb.append(",\"columnCname\":{\"type\":\"keyword\"}");
        sb.append(",\"columnEname\":{\"type\":\"keyword\"}");
        sb.append(",\"primaryKey\":{\"type\":\"keyword\"}");
        sb.append("}}}");
        return sb.toString();
    }

    private String getLogoperationMapping(){
        StringBuilder sb = new StringBuilder();
        sb.append("{\"doc\":{\"properties\":{");
        sb.append("\"logId\":{\"type\":\"keyword\"}");
        sb.append(",\"modelId\":{\"type\":\"keyword\"}");
        sb.append(",\"modelCname\":{\"type\":\"keyword\"}");
        sb.append(",\"modelBussiness\":{\"type\":\"keyword\"}");
        sb.append(",\"modelSysname\":{\"type\":\"keyword\"}");
        sb.append(",\"logType\":{\"type\":\"keyword\"}");
        sb.append(",\"orgId\":{\"type\":\"keyword\"}");
        sb.append(",\"orgEname\":{\"type\":\"keyword\"}");
        sb.append(",\"createdTime\":{\"type\":\"date\"}");
        sb.append(",\"createUser\":{\"type\":\"keyword\"}");
        sb.append(",\"logIp\":{\"type\":\"keyword\"}");
        sb.append(",\"primaryKey\":{\"type\":\"keyword\"}");
        sb.append(",\"details\":{\"type\":\"text\"}");
        sb.append("}}}");
        return sb.toString();
    }
}
